/**
TODO: SessionID or RoomID
Pass the session id to server..
**/
define([], function () {

    return {
    	/* Desc : group will be deleted after export */
    	renderGroupToImage : function(group){
			var bound = App.kinetic.operation.getBoundaryFromGroup(group);
			if(!App.kinetic.editMode){
				var oldX = bound.x;
				var oldY = bound.y;
				var exporterStage = App.kinetic.operation.prepareGroupForExport(bound,group);
			}else{

				var oldX = App.toolbox.editedProp.x;
				var oldY = App.toolbox.editedProp.y;
				bound = {
					width:App.toolbox.editedProp.width,
					height:App.toolbox.editedProp.height,
				};
				var exporterStage = App.kinetic.operation.prepareGroupForExport(bound,group);
			}
    		
			console.log(exporterStage);
			group.toImage({
				callback: function(img) {
					var timeName = (new Date).getTime();
					exporterStage.destroy();
					var objTyp = "shape";
					if(!App.kinetic.editMode){
						App.addObjectToServer(img.src,oldX,oldY-20,objTyp);
					}else{
						App.updateImage(img.src,App.editProp.y-$('#rootContainer').offset().top,App.editProp.x-$('#rootContainer').offset().left,objTyp,App.toolbox.editedId);
					}
				}
			});
    	},
    	
    	prepareGroupForExport : function(bound,group){
			var stage = new Kinetic.Stage({
				container: App.kineticExporterCanvas,
				width: bound.width,
				height: bound.height
			});
			
			var layer = new Kinetic.Layer();
			layer.add(new Kinetic.Rect({
				x:0,
				y:0,
				width: bound.width,
				height: bound.height
			}));
			
			group.setX(-bound.x);
			group.setY(-bound.y);
			stage.add(layer);
			group.moveTo(layer);
			
			return stage;
		},
    	
    	getBoundaryFromGroup : function(group){
			var boundary = {x:0,y:0,width:0,height:0};
			var minX = App.kinetic.stageWidth,minY = App.kinetic.stageHeight,maxX = 0,maxY = 0;
			var strokeWidth = 0;
			group.getChildren().forEach(function(child){
				strokeWidth = parseInt((child.attrs.strokeWidth+1)/2);
				if(child.shapeType == "Line"){
					child.attrs.points.forEach(function(point){
						if((point.x-strokeWidth)<minX)
							minX = point.x-strokeWidth;
						if((point.y-strokeWidth)<minY)
							minY = point.y-strokeWidth;
						if((point.x+strokeWidth)>maxX)
							maxX = point.x+strokeWidth;
						if((point.y+strokeWidth)>maxY)
							maxY = point.y+strokeWidth;
					});
				}else{
					if(child.shapeType == "Ellipse"){
						console.log("child.getX()");
						console.log(child.getX());
						console.log("child.getY()");
						console.log(child.getY());
						console.log("child.getRadius()");
						console.log(child.getRadius());
						console.log("strokeWidth");
						console.log(strokeWidth);
						
						if(child.getX() - parseInt(child.getRadius().x) - strokeWidth<minX)
							minX = child.getX() - parseInt(child.getRadius().x) - strokeWidth;
						if(child.getY() - parseInt(child.getRadius().y) - strokeWidth<minY)
							minY = child.getY() - parseInt(child.getRadius().y) - strokeWidth;
						if(child.getX()+parseInt(child.getRadius().x) + strokeWidth >maxX)
							maxX = child.getX()+child.getWidth() + strokeWidth;
						if(child.getY() + parseInt(child.getRadius().y) + strokeWidth>maxY)
							maxY = child.getY() + child.getHeight() + strokeWidth;
					}
					// if(child.shapeType == "Ellipse"){
						// if(child.getX() - parseInt(child.getRadius().x) - strokeWidth<minX)
							// minX = child.getX() - parseInt(child.getRadius().x) - strokeWidth;
						// if(child.getY() - parseInt(child.getRadius().y) - strokeWidth<minY)
							// minY = child.getY() - parseInt(child.getRadius().y) - strokeWidth;
						// if(child.getX()+child.getWidth() + strokeWidth >maxX)
							// maxX = child.getX()+child.getWidth() + strokeWidth;
						// if(child.getY() + child.getHeight() + strokeWidth>maxY)
							// maxY = child.getY() + child.getHeight() + strokeWidth;
					// }
					else{
						if(child.getX() - strokeWidth<minX)
							minX = child.getX() - strokeWidth;
						if(child.getY() - strokeWidth<minY)
							minY = child.getY() - strokeWidth;
						if(child.getX()+child.getWidth() + strokeWidth>maxX)
							maxX = child.getX()+child.getWidth() + strokeWidth;
						if(child.getY() + child.getHeight() + strokeWidth>maxY)
							maxY = child.getY() + child.getHeight() + strokeWidth;
					}
				}
			});
			if(minX<0 || isNaN(minX))minX = 0;
			if(minY<0 || isNaN(minY))minY = 0;
		
			
			boundary.x = minX;
			boundary.y = minY;
			boundary.width = maxX - minX;
			boundary.height = maxY - minY;
			
			return boundary;
		}
	}
}); 